

clear all

cd "$datadir" 

clear all

use "CA_dist_to_BG_UNIQUE_w_demos.dta", replace

 
gen total_income=pc_inc_yr*ratio_inc_pov_tot

rename pc_inc_yr og_pc_inc_yr

**************************

collapse (sum) totalpop pop_urban pop_rural pop_r_white pop_r_black pop_r_natam pop_r_other pop_r_hisp pop_male pop_female pop_r_asipac pop_over18 pop_over65 totalhh hh_1male hh_1female hh_mar_wc hh_mar_nc hh_male_wc hh_male_nc hh_female_wc hh_female_nc hh_nonfam_male hh_nonfam_female housing_units occ_housing vac_housing renter_occ_housing owner_occ_housing land_sqkm ratio_inc_pov_tot ratio_inc_pov_1 ratio_inc_pov_2 ratio_inc_pov_3 ratio_inc_pov_4 ratio_inc_pov_5 ratio_inc_pov_6 ratio_inc_pov_7 pov_total bpov_yr pova_yr med_hhinc total_income recSNAP_total recSNAPyr recSNAPyr_disabled recSNAPyr_nodisabled noSNAPyr noSNAPyr_disabled noSNAPyr_nodisabled pres16_dem pres16_rep cvap_aapi cvap_black cvap_white cvap_hisp cvap_other cvap_total, by (city city_council_commissioner_distri)
 
**keep track of which data go with which level (hh, pop, etc)
drop if city==""
drop if city_council=="" 
isid city city_council_commissioner_distri

* generate district population demographic shares (race and age; poverty status)
 foreach x in pop_urban pop_rural pop_r_white pop_r_black pop_r_natam pop_r_other pop_r_hisp pop_male pop_female  pop_r_asipac pop_over18 pop_over65 {
 	
	gen share_`x'=`x'/totalpop
	
 }
 
 gen pc_inc_yr=total_income/ratio_inc_pov_tot
 
foreach x in bpov_yr pova_yr {
 	
	gen share_`x'=`x'/pov_total
 }
* generate district household demographic shares (sex/children)
foreach x in hh_1male hh_1female hh_mar_wc hh_mar_nc hh_male_wc hh_male_nc hh_female_wc hh_female_nc hh_nonfam_male hh_nonfam_female {
 	
	gen share_`x'=`x'/totalhh
	
 } 
* generate district housing type shares (by total units) 
foreach x in occ_housing vac_housing renter_occ_housing owner_occ_housing {
 	
	gen share_`x'=`x'/housing_units
	
 }
* generate district Welfare benefit shares (measured by household)
foreach x in recSNAPyr recSNAPyr_disabled recSNAPyr_nodisabled noSNAPyr noSNAPyr_disabled noSNAPyr_nodisabled {
 	
	gen share_`x'=`x'/recSNAP_total
 }	
* generate district poverty ratio shares (measured by household)
foreach x in ratio_inc_pov_1 ratio_inc_pov_2 ratio_inc_pov_3 ratio_inc_pov_4 ratio_inc_pov_5 ratio_inc_pov_6 ratio_inc_pov_7 {
 	
	gen share_`x'=`x'/ratio_inc_pov_tot
 }
 
 * CVAPs
 
 foreach x in cvap_aapi cvap_black cvap_white cvap_hisp cvap_other {
 	gen share_`x'=`x'/cvap_total
 } 
 
 **cumulative income:poverty shares 
 gen cumulative_share_rip_1 = share_ratio_inc_pov_1
 gen cumulative_share_rip_2 = share_ratio_inc_pov_1 + share_ratio_inc_pov_2
 gen cumulative_share_rip_3 = cumulative_share_rip_2 + share_ratio_inc_pov_3
 gen cumulative_share_rip_4 = cumulative_share_rip_3 + share_ratio_inc_pov_4
 gen cumulative_share_rip_5 = cumulative_share_rip_4 + share_ratio_inc_pov_5
 gen cumulative_share_rip_6 = cumulative_share_rip_5 + share_ratio_inc_pov_6
 gen cumulative_share_rip_7 = cumulative_share_rip_6 + share_ratio_inc_pov_7

* identify MAJORITY sex by district
foreach x in male female {
	gen maj_`x'=share_pop_`x'>0.5 if share_pop_`x'!=.
}

* identify MAJORITY race/eth groups by district
foreach x in white black natam other hisp asipac {
	gen maj_r_`x'=share_pop_r_`x'>0.5 if share_pop_r_`x'!=.
}

foreach x in white black   other hisp aapi {
	gen maj_cvap_`x'=share_cvap_`x'>0.5 if share_cvap_`x'!=.
}

* identify MODAL race/eth groups by district
egen max_share_r=rowmax(share_pop_r_white share_pop_r_black share_pop_r_natam share_pop_r_other share_pop_r_hisp share_pop_r_asipac)
foreach x in white black natam other hisp asipac {
	gen modal_r_`x'=share_pop_r_`x'==max_share_r
}

* identify majority Income:Poverty by district
foreach x in 1 2 3 4 5 6 7 {
	gen maj_ratio_inc_pov_`x'=share_ratio_inc_pov_`x'>0.5 if share_ratio_inc_pov_`x'!=.
}

* identify median Income:Poverty level in district
gen med_rip =. 
foreach x in 7 6 5 4 3 2 1 {
	replace med_rip = `x' if cumulative_share_rip_`x'>0.5 
}

quietly tabulate med_rip, generate (med_rip_)
rename med_rip_6 med_rip_7
rename med_rip_5 med_rip_6 
rename med_rip_4 med_rip_5
rename med_rip_3 med_rip_4 
rename med_rip_2 med_rip_3 
rename med_rip_1 med_rip_2 

*identify districts where a MAJORITY rec'd SNAP
foreach x in recSNAPyr recSNAPyr_disabled recSNAPyr_nodisabled noSNAPyr noSNAPyr_disabled noSNAPyr_nodisabled {
	gen maj_`x'=share_`x'>0.5 if share_`x'!=.
}

*identify districts where a MODALITY rec'd SNAP
egen max_share_SNAP=rowmax(share_recSNAPyr share_recSNAPyr_disabled share_recSNAPyr_nodisabled share_noSNAPyr share_noSNAPyr_disabled share_noSNAPyr_nodisabled)
foreach x in recSNAPyr recSNAPyr_disabled recSNAPyr_nodisabled noSNAPyr noSNAPyr_disabled noSNAPyr_nodisabled {
	gen modal_`x'=share_`x'==max_share_SNAP
}

 

*identify district MAJORITY housing occupancy type (by unit #) 
foreach x in renter_occ_housing owner_occ_housing {
 	
	gen maj_`x'=share_`x'>0.5 if share_`x'!=.
	
 }
*identify district MODALITY housing occupancy type (by unit #) 
egen max_share_housing=rowmax(share_renter_occ_housing share_owner_occ_housing)
foreach x in renter_occ_housing owner_occ_housing {
	gen modal_`x'=share_`x'==max_share_housing
}
  

 
 gen maj_dem=pres16_dem/(pres16_dem+pres16_rep)>0.5
   gen pres16_demshare=pres16_dem/(pres16_dem+pres16_rep)

     
     gen Uplace=proper(city)
	 
	 egen cXd=group(city city_council_commissioner_distri)
 
capture  drop _merge

  
 * merge in distrct dates
reclink2  Uplace using CA_all_districted_cities_2008_2020_w_placefips_for_merge_sept2024, gen(matchscore) idmaster(cXd) idusing(placefips)

  
keep if   _merge==3

 drop if placefips==.
 

gen med_hhinc_1k=med_hhinc /1000 
gen pc_inc_yr_1k=  pc_inc_yr /1000
gen totalpop_1k=totalpop/1000


* adding measures of variation in economic segregation

	* measured using individual income ratio
	
	bysort city: egen city_ratio_inc_pov_7=total(ratio_inc_pov_7)
	egen ratio_inc_pov_1_6=rowtotal(ratio_inc_pov_1-ratio_inc_pov_6)
	bysort city: egen city_ratio_inc_pov_1_6=total(ratio_inc_pov_1_6)
	gen ds_abs_gap=abs( (ratio_inc_pov_7/city_ratio_inc_pov_7)-(ratio_inc_pov_1_6/city_ratio_inc_pov_1_6))

	bysort city: egen sum_ds_abs_gap=total(ds_abs_gap)
	gen high_inc_dissim=0.5*sum_ds_abs_gap

	drop sum_ds_abs_gap ds_abs_gap city_ratio_inc_pov_7 city_ratio_inc_pov_1_6

	* measured using household poverty
	
	bysort city: egen city_pova_yr=total(pova_yr)
	bysort city: egen city_bpov_yr=total(bpov_yr)
	gen ds_abs_gap=abs( (pova_yr/city_pova_yr)-(bpov_yr/city_bpov_yr))

	bysort city: egen sum_ds_abs_gap=total(ds_abs_gap)
	gen pov_dissim=0.5*sum_ds_abs_gap

	drop sum_ds_abs_gap ds_abs_gap city_pova_yr city_bpov_yr

	
	* within city variation in per capita income
	bysort city: egen city_sd_pc_inc_yr=sd(pc_inc_yr)
	rename high_inc_dissim high_inc_dissim_D
	rename pov_dissim pov_dissim_D
	rename city_sd_pc_inc_yr city_sd_pc_inc_D
 

label var totalpop_1k "Pop. (1000s)"
label var share_pop_r_white  "Pct. White (Non-Hisp)"
label var share_pop_r_black "Pct. Black (Non-Hisp)"
label var share_pop_r_asipac "Pct. AAPI (Non-Hisp)"
label var share_pop_r_hisp "Pct. Hisp."
label var med_hhinc_1k "Med. Inc. (1000s)"
label var pc_inc_yr_1k  "Per Cap. Inc. (1000s)"
label var share_renter_occ_housing "Pct. Renter"
label var share_ratio_inc_pov_7  "Pct. > 2x Pov. Line"
label var pres16_demshare  "Pct. Obama Share (2016)"

*label variable c_totalpop_1k "Total Pop. (1000s)"
label variable share_pop_over18 "Share Over 18"
label variable share_pop_over65 "Share Over 18"
label variable share_pop_over65 "Share Over 65"
label variable share_renter_occ_housing "Share Renter Occ. Housing"
label variable share_ratio_inc_pov_7 "Share Inc > 2x Pov."
label variable share_cvap_aapi "Share AAPI (CVAP)"
label variable share_cvap_black "Share Black (CVAP)"
label variable share_cvap_white "Share White (CVAP)"
label variable share_cvap_hisp "Share AAPI (CVAP)"
label variable share_cvap_hisp "Share Hisp. (CVAP)"
label variable share_cvap_other "Share Other Race/Eth. (CVAP)"
 
label var maj_r_white "District Race Composition"
label define dummy 0 "No" 1 "Yes" 
label define maj_r_white 0 "Not Maj. White" 1 "Maj. White" 

label values maj_r_white maj_r_white

label variable maj_cvap_white "Maj. White (CVAP)"
label values maj_cvap_white maj_r_white

cd "$dist_output"

  dtable    totalpop_1k share_cvap_aapi share_cvap_black share_cvap_white share_cvap_hisp share_cvap_other      share_renter_occ_housing share_ratio_inc_pov_7   if switch==1 , by(maj_cvap_white)  varlabel   nformat(%9.2f)  export(sumstats.tex, replace tableonly) 
  
  
 